function luminosity(color)
{
    // http://www.w3.org/TR/WCAG20/#relativeluminancedef
    const red = color.r
    const green = color.g
    const blue = color.b

    const redLum = (red <= 0.03928) ? red / 12.92 : Math.pow(((red + 0.055) / 1.055), 2.4)
    const greenLum = (green <= 0.03928) ? green / 12.92 : Math.pow(((green + 0.055) / 1.055), 2.4)
    const blueLum = (blue <= 0.03928) ? blue / 12.92 : Math.pow(((blue + 0.055) / 1.055), 2.4)

    return 0.2126 * redLum + 0.7152 * greenLum + 0.0722 * blueLum
}

function contrast(color1, color2)
{
    const lum1 = luminosity(color1)
    const lum2 = luminosity(color2)

    if (lum1 > lum2) {
        return (lum1 + 0.05) / (lum2 + 0.05)
    }

    return (lum2 + 0.05) / (lum1 + 0.05)
}

function display_text_color()
{
    if (contrast(Kontrast.color, Kontrast.bkcolor) > 3.0) {
        return Kontrast.color;
    }

    if (luminosity(Kontrast.bkcolor) > 0.5) {
        return 'black'
    }
    return 'white'
}

function random_color() {
    do {
        var color_text = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
        var color_background = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
    } while (contrast(color_text, color_background) < 3.5);

    Kontrast.color = color_text
    Kontrast.bkcolor = color_background
}

function get_contrast_qualities(contrast)
{
    if (contrast > 7.0) {
        return {'small': 'Perfect', 'medium': 'Perfect', 'large': 'Perfect'};
    } else if (contrast > 4.5) {
        return {'small': 'Good', 'medium': 'Good', 'large': 'Perfect'};
    } else if (contrast > 3.0) {
        return {'small': 'Bad', 'medium': 'Bad', 'large': 'Good'};
    } else {
        return {'small': 'Bad', 'medium': 'Bad', 'large': 'Bad'};
    }
}

function get_font_size_quality_label(contrast, font_size)
{
    const current_qualities = get_contrast_qualities(contrast);

    var current_quality;
    if (font_size >= 18) {
        current_quality = current_qualities['large'];
    } else if (font_size > 13) {
        current_quality = current_qualities['medium'];
    } else {
        current_quality = current_qualities['small'];
    }
    return `Font size ${font_size} is ${current_quality} with the current contrast`
}
